package com.jinxin.platform.cdinfogo.utils;

import java.text.DecimalFormat;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description： 数据类型转换工具类
 * @Date: 2021-06-10
 * @Author: Lingnan
 **/
public class NumConvertUtil {
    /**
     * bytes 转16进制字符串
     * @param bArray
     * @return
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
    /**
     * 16进制字符串转bytes
     * @param hex
     * @return
     */
    public static byte[] hexStringToByte(String hex) {
        int len = 0;
        int num=0;

        //判断字符串的长度是否是两位
        if(hex.length()>=2){

            //判断字符喜欢是否是偶数
            len=(hex.length() / 2);
            num = (hex.length() % 2);

            if (num == 1) {
                hex = "0" + hex;
                len=len+1;
            }


        }else{

            hex = "0" + hex;
            len=1;


        }


        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;



    }

    private static int toByte(char c) {
        if (c >= 'a')
            return (c - 'a' + 10) & 0x0f;
        if (c >= 'A')
            return (c - 'A' + 10) & 0x0f;
        return (c - '0') & 0x0f;
    }

    /**
     * 16进制字符串转十进制指定位数的string
     * @param hexString
     * @return
     */
    public static String hexStringToInt(String hexString,int size) {

        String s = Integer.valueOf(hexString.replace(" ",""), 16)+"";
        StringBuffer sb = new StringBuffer();
        if(s.length() < size) {
            for (int i = 0; i < (size - s.length()); i++) {
                sb.append("0");
            }
            sb.append(s);
            return sb.toString();
        }
        return s;
    }


    public static int hexStringToInt(String hexString) {
        return Integer.valueOf(hexString.replace(" ",""), 16);
    }

    /**
     * 16进制字符串转十进制double
     * @param hexString
     * @return
     */
    public static double hexStringToDouble(String hexString) {
        return Double.longBitsToDouble(Long.parseLong(hexString.replace(" ",""), 16));
    }

    /**
     * 十进制int转16进制字符串
     * @param num
     * @param size 位数
     * @return
     */
    public static String intToHexString(int num,int size) {

        String s = Integer.toHexString(num).toUpperCase();
        StringBuffer sb = new StringBuffer();
        if(s.length() < size) {
            for (int i = 0; i < (size - s.length()); i++) {
                sb.append("0");
            }
            sb.append(s);
            return sb.toString();
        }
        return s;
    }

    /**
     * 字符串转化成为16进制字符串
     * @param s
     * @return
     */
    public static String strToHexString(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str.toUpperCase();
    }

    /**
     * 16进制String转BCD
     * @param asc
     * @return
     */
    public static byte[] strToBcd(String asc) {
        int len = asc.length();
        int mod = len % 2;

        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }

        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }

        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;

        for (int p = 0; p < asc.length()/2; p++) {
            if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }

            if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            }else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }

            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }
        return bbt;
    }


    /**
     * String 类型数字转化为Double  保留置顶小数位(用于显示金额等。)
     * @param money
     * @param type  保留小数点位数  #.00保留两位  #.0保留一位  #保留整数
     * @return
     */
    public static String strToDouble(String money,String type){

        String toDouble= new DecimalFormat(type).format(Double.parseDouble(money));

        return toDouble;

    }

    public static byte[] intToByte(int in){
        byte [] b=new byte[4];
        b[3]= (byte) (in&0xff);
        b[2]= (byte) (in>>8 &0xff);
        b[1]= (byte) (in>>16 &0xff);
        b[0]= (byte) (in>>24 &0xff);
        return b;
    }

    // 16进制转2进制
    public static String hexString2binaryString(String hexString,int size) {
        int sint=Integer.valueOf(hexString.replace(" ",""), 16);
        //十进制在转换成二进制的字符串形式输出!
        String s = Integer.toBinaryString(sint);
        StringBuffer sb = new StringBuffer();
        if(s.length() < size){
            for (int i = 0; i < (size - s.length()); i++) {
                sb.append("0");
            }
            return sb.append(s).toString();
        }
        return s;
    }

    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     * @param hexStr
     * @return
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
     * 16进制表示的字符串转换为字节数组
     * 返回的字节数组表示为十进制
     *
     * @param hexString 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    /**

     * 字符串转换成为16进制(无需Unicode编码)

     * @param str

     * @return

     */

    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();

        StringBuilder sb = new StringBuilder("");

        byte[] bs = str.getBytes();

        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;

            sb.append(chars[bit]);

            bit = bs[i] & 0x0f;

            sb.append(chars[bit]);

            // sb.append(' ');

        }

        return sb.toString().trim();

    }

    /**
     * 字节数组转16进制
     * @param bytes 需要转换的byte数组
     * @param length 处理的位数
     * @return  转换后的Hex字符串
     */
    public static String bytesToHex(byte[] bytes,int length) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if(hex.length() < 2){
                sb.append(0);
            }
            sb.append(hex.toUpperCase()+" ");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(hexStringToInt("01C3BD28"));
        System.out.println(hexStringToDouble("01C3BD28"));
        System.out.println(hexStringToInt("00 00 00 00 01",8));
//        System.out.println(hexStr2Str("0000AAAAAA"));
//        String s1 = intToHexString(2130903039,8);
//        System.out.println(s1); //7f02ffff
//
//        System.out.println(strToHexString("MC-5824T20114036"));
//        System.out.println(intToHexString(1,8));
//        System.out.println(intToHexString(10,8));
//        System.out.println(hexStringToByteArray("FFFFFFFF"));
//        int num = Integer.parseInt("00000010", 16);
//        System.out.println(hexStringToInt("0000AAAAAA"));
//        System.out.println(intToHexString(11184810,10));
//        System.out.println(hexStringToInt("00 01".replace(" ","")));
//        System.out.println(hexStringToDouble("10 01".replace(" ","")));
//        String s = hexString2binaryString("0013", 8);
//        System.out.println(s);
//        int i1 = Integer.parseInt(String.valueOf(s.charAt(5)));
//        int i2 = Integer.parseInt(String.valueOf(s.charAt(6)));
//        int i3 = Integer.parseInt(String.valueOf(s.charAt(7)));
//        System.out.println(i1);
//        System.out.println(i2);
//        System.out.println(i3);
//        System.out.println("aaaa" + LocalDateTime.now());


        String str = "00 00 00 01 00 00 00 00 01 20 11 13 16 47 34 03 01 00 00 00 02 00 00 AA AA AA 20 11 13 16 47 34 05 0C 00 00 00 03 00 00 AA AA AA 20 11 13 16 47 34 06 0C 00 00 00 04 00 00 AA AA AA 20 11 13 16 47 34 07 0C 00 00 00 05 00 00 AA AA AA 20 11 13 16 47 34 08 0C 00 00 00 06 00 00 AA FF AA 20 11 13 16 47 45 01 0C 00 00 00 07 00 00 AA FF AA 20 11 13 16 47 45 02 0C 00 00 00 08 00 00 AA FF AA 20 11 13 16 47 45 03 0C 00 00 00 09 00 00 7F FF FF 20 11 13 16 47 46 01 01 00 00 00 0A 00 00 7F FF FF 20 11 13 16 47 46 02 01 C5 7E 7E 00 00 00 17 4D 43 2D 35 38 32 34 54 32 30 31 31 34 30 33 36 FF FF FF FF 38 04 FF 00 00 00 04 00 00 00 0A";
        int length = str.length();
        int n = length / 50;
//        System.out.println(n);
        for (int i = 0; i < 10; i++) {
            String lineStr = str.substring(50 * i + i, 50 * (i + 1) + i);
            String index = lineStr.substring(0, 11).replace(" ","");
            String cardNum = lineStr.substring(12, 26).replace(" ","");
//            System.out.println(cardNum);
            if("FFFFFFFFFF".equals(cardNum)){
                break;
            }
            String timeStr = lineStr.substring(27, 44).replace(" ","");
            String doorStr = lineStr.substring(45, 47).replace(" ","");
            String status = lineStr.substring(48, 50).replace(" ","");
            System.out.println(index + "-" +cardNum + "-" + timeStr + "-" + doorStr + "-" + status);
        }
//        System.out.println(str.substring(50*0+0,50*1+0)); // 0,50
//        System.out.println(str.substring(50*1+1,50*2+1)); // 51,101
//        System.out.println(str.substring(50*2+2,50*3+2)); // 102,152
//        System.out.println(str.substring(50*3+3,50*4+3)); // 153,203
    }
}
